<template>
  <div class="ma-content-block">
    <sa-table ref="crudRef" :options="options" :columns="columns" :searchForm="searchForm">
      <!-- 搜索区 tableSearch -->
      <template #tableSearch>
        <a-col :span="8">
          <a-form-item label="讲者简历" field="speaker_resume">
            <a-input v-model="searchForm.speaker_resume" placeholder="请输入讲者简历" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="讲者简历-英文" field="speaker_resume_en">
            <a-input v-model="searchForm.speaker_resume_en" placeholder="请输入讲者简历-英文" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="常旅客号" field="passenger_number">
            <a-input v-model="searchForm.passenger_number" placeholder="请输入常旅客号" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="" field="status">
            <a-input v-model="searchForm.status" placeholder="请输入" />
          </a-form-item>
        </a-col>
      </template>

      <!-- Table 自定义渲染 -->
      <template #status="{ record }">
        <sa-switch v-model="record.status" @change="changeStatus($event, record.id)"></sa-switch>
      </template>
    </sa-table>

    <!-- 编辑表单 -->
    <edit-form ref="editRef" @success="refresh" />
  </div>
</template>

<script setup>
import { onMounted, ref, reactive } from 'vue'
import { Message } from '@arco-design/web-vue'
import EditForm from './edit.vue'
import api from '../api/resume'

// 引用定义
const crudRef = ref()
const editRef = ref()

// 搜索表单
const searchForm = ref({
  speaker_resume: '',
  speaker_resume_en: '',
  passenger_number: '',
  status: '',
})

const changeStatus = async (status, id) => {
  const response = await api.changeStatus({ id, status })
  if (response.code === 200) {
    Message.success(response.message)
    crudRef.value.refresh()
  }
}

// SaTable 基础配置
const options = reactive({
  api: api.getPageList,
  recycleApi: api.getRecyclePageList,
  rowSelection: { showCheckedAll: true },
  add: {
    show: true,
    auth: ['/cms/SpeakerResume/save'],
    func: async () => {
      editRef.value?.open()
    },
  },
  edit: {
    show: true,
    auth: ['/cms/SpeakerResume/update'],
    func: async (record) => {
      editRef.value?.open('edit')
      editRef.value?.setFormData(record)
    },
  },
  delete: {
    show: true,
    auth: ['/cms/SpeakerResume/destroy'],
    func: async (params) => {
      const resp = await api.delete(params)
      if (resp.code === 200) {
        Message.success(`删除成功！`)
        crudRef.value?.refresh()
      }
    },
    realAuth: ['/cms/SpeakerResume/realDestroy'],
    realFunc: async (params) => {
      const resp = await api.realDestroy(params)
      if (resp.code === 200) {
        Message.success(`销毁成功！`)
        crudRef.value?.refresh()
      }
    },
  },
  recovery: {
    show: true,
    auth: ['/cms/SpeakerResume/recovery'],
    func: async (params) => {
      const resp = await api.recovery(params)
      if (resp.code === 200) {
        Message.success(`恢复成功！`)
        crudRef.value?.refresh()
      }
    },
  },
})

// SaTable 列配置
const columns = reactive([
  { title: '讲者id', dataIndex: 'speaker_id', width: 180 },
  { title: '讲者简历', dataIndex: 'speaker_resume', width: 180 },
  { title: '讲者简历-英文', dataIndex: 'speaker_resume_en', width: 180 },
  { title: '讲者ppt', dataIndex: 'speaker_ppt', width: 180 },
  { title: '讲者ppt-英文', dataIndex: 'speaker_ppt_en', width: 180 },
  { title: '讲者护照', dataIndex: 'speaker_passport', width: 180 },
  { title: '汇款信息', dataIndex: 'remittance', width: 180 },
  { title: '常旅客号', dataIndex: 'passenger_number', width: 180 },
  { title: '', dataIndex: 'status', width: 180 },
])

// 页面数据初始化
const initPage = async () => {}

// SaTable 数据请求
const refresh = async () => {
  crudRef.value?.refresh()
}

// 页面加载完成执行
onMounted(async () => {
  initPage()
  refresh()
})
</script>
